package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service(interfaceClass= PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    //本地注入
    @Autowired
    private PermissionDao permissionDao;

    //权限分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        //获取一页几行
        Integer pageSize = queryPageBean.getPageSize();
        //获取当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取查找的字符串
        String queryString = queryPageBean.getQueryString();
        //利用分页助手
        PageHelper.startPage(currentPage,pageSize);
        //调用dao层方法
        Page<Permission> page = permissionDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Permission> rows = page.getResult();
        return new PageResult(total,rows);
    }

    //按Id查询
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    //添加权限
    @Override
    public void add(Permission permission)throws Exception {
        //获取现在时间
        String creationdate = DateUtils.parseDate2String(new Date());
        //简时间赋值给权限对象
        permission.setCreationdate(creationdate);
        permissionDao.add(permission);
    }

    //删除
    @Override
    public void delete(Integer id) {
        permissionDao.delete(id);
    }

    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }
}
